package com.hongkou.tvstation.support.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.hongkou.tvstation.pub.hibernate.GenericDaoSupport;
import com.hongkou.tvstation.support.data.TUser;

public class UserDao extends GenericDaoSupport {
	private static final String SQL__USER_LOGIN = "login";

	public TUser findUser(String username, String password) throws SQLException {
		Connection connection = getMySqlConnection();
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		TUser tUser = null;
		statement = connection
				.prepareStatement("select * from t_user where user_name=? and password=?");
		int point = 1;
		statement.setString(point++, username);
		statement.setString(point++, password);
		resultSet = statement.executeQuery();
		if (resultSet.next()) {
			tUser = new TUser();
			tUser.setUserName(username);
			tUser.setPassword(password);
		}
		return tUser;
	}

	public void initTUser() throws SQLException {
		Connection connection = getMySqlConnection();
		Connection accessConn = getAccessConnection();
		Statement statement = null;
		PreparedStatement insertStatement = null;
		ResultSet resultSet = null;
		try {
			connection.setAutoCommit(false);
			resultSet = accessConn.createStatement().executeQuery(
					"select name,xingming from admin group by name,xingming");
			statement = connection.createStatement();
			statement.executeUpdate("delete from t_user");
			insertStatement = connection
					.prepareStatement("insert into t_user(user_name,password,xingming) values(?,?,?)");
			while (resultSet.next()) {
				insertStatement.setString(1, resultSet.getString("name"));
				insertStatement.setString(2, "pass");
				insertStatement.setString(3, resultSet.getString("xingming"));
				insertStatement.executeUpdate();
			}
			connection.commit();
		} catch (SQLException e) {
			throw e;
		} finally {
			close(null, statement, null);
			close(resultSet, insertStatement, connection);
		}

	}
}
